//
//  main.m
//  04_HomeWorkColor
//
//  Created by dllo on 16/7/7.
//  Copyright © 2016年 LST. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
    /*
     处理⽂件crayons.txt中的⽂本信息，⽂本内容是关于颜⾊的，每⾏都是⼀ 个颜⾊的信息，例如：Almond #EED9C4，前⼀个字符串是颜⾊的名称，后⼀ 个字符串是颜⾊的16进制⾊值，处理⽂本完成如下需求:
     
     使⽤字典管理所有的颜⾊，即字典中存储的是多个键值对，颜⾊名称为 key，16进制颜⾊值（不带#）是value。
     使⽤⼀个新的字典管理颜⾊，对颜⾊进⾏分类管理，即：“A”，“B”，“C”… 即这个字典包含多个键值对，key是26个字⺟，value是数组，数组⾥存放的是 Color对象（包含name和colorValue）。需要⾃⼰创建Color类。
     获取文件中字符串的方法如下:
     
     NSString *path = [[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt"];
     NSString *sourceStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
     根据得到的sourceStr进行处理*/
#if 0
    {
        NSString * dataStr = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/ClassCode/04_HomeWorkColor/04_HomeWorkColor/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
        // NSLog(@"%@", dataStr);
        NSArray *arr = [dataStr componentsSeparatedByString:@"\n"];
        //NSLog(@"%@", arr);
        NSMutableArray *arr1 = [NSMutableArray array];
        for (int i = 0; i < arr.count; i++) {
            NSArray  *arr2 = [arr[i] componentsSeparatedByString:@" #"];
            [arr1 addObjectsFromArray:arr2];
        }
        // NSLog(@"%@", arr1);
        NSMutableArray *array1 = [NSMutableArray array];
        NSMutableArray *array2 = [NSMutableArray array];
        for (int i = 0; i < arr1.count; i++) {
            if ((i + 2) % 2 == 0) {
                [array1 addObject:arr1[i]];
            } else {
                [array2 addObject:arr1[i]];
            }
        }
        //    NSLog(@"%@",array1);
        NSMutableDictionary *dicKey = [NSMutableDictionary dictionaryWithObjects:array2 forKeys:array1];
        //® NSLog(@"%@", dicKey);
    }
#endif
#if 0
    {
        NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/04_HomeWorkColor/04_HomeWorkColor/crayons.txt";
        // 通过文件路径读取文件中的字符串
        NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@", sourceString);
        // 根据\n 切割字符串
        NSArray *colorStringArray = [sourceString componentsSeparatedByString:@"\n"];
        NSLog(@"%@", colorStringArray);
        // 声明存储颜色的可变字典
        NSMutableDictionary *colorDictionary = [NSMutableDictionary dictionary];
        for (int i = 0; i < colorStringArray.count; i++) {
            if ([colorStringArray[i] length] > 0) {
                NSArray *color = [colorStringArray[i] componentsSeparatedByString:@" #"];
                [colorDictionary setObject:[color lastObject] forKey:[color firstObject]];
            }
        }
        NSLog(@"%@", colorDictionary);
        // 创建新字典
        NSMutableDictionary *newColorDic = [NSMutableDictionary dictionary];
        // 取到老字典中所有key值
        NSArray *allKeys = colorDictionary.allKeys;
        // 取首字母
        for (int i = 0; i < allKeys.count; i++) {
            NSString *subString = [allKeys[i] substringWithRange:NSMakeRange(0, 1)];
            // 通过首字母取出对应的Array
            NSMutableArray *colorArray = [newColorDic objectForKey:subString];
            // 根据老字典的key和value生成新的color对象
            Color *newColor = [[Color alloc] init];
            newColor.name = allKeys[i];
            newColor.colorValue = [colorDictionary objectForKey:allKeys[i]];
            // 判断从新字典中取出的数组是否为空
            // 为空说明还没有存过对应首字母的数组
            if (colorArray == nil) {
                // 创建
                colorArray = [NSMutableArray arrayWithObject:newColor];
                // 赋值给对应的首字母
                [newColorDic setObject:colorArray forKey:subString];
            } else {
                // 如果数组不为空, 直接添加
                [colorArray addObject:newColor];
            }
        }
        NSLog(@"%@", newColorDic);
    }
#endif
#if 0
    {
        // 2.
        NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/ClassCode/04_HomeWorkColor/04_HomeWorkColor/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
        //    NSLog(@"%@", dataString);
        NSArray *colorArr = [dataString componentsSeparatedByString:@"\n"];
        NSMutableArray *colorArray = [[NSMutableArray alloc] init];
        for (NSString *string in colorArr) {
            NSArray *arr = [string componentsSeparatedByString:@" #"];
            Color *color = [[Color alloc] initWithName:arr[0] colorValue:arr[1]];
            [colorArray addObject:color];
        }
        //    NSLog(@"%@", colorArray);
        NSMutableDictionary *colorDic = [[NSMutableDictionary alloc] init];
        for (Color *color in colorArray) {
            unichar capital = [[color.name capitalizedString] characterAtIndex:0];
            NSString *group = [NSString stringWithFormat:@"%c", capital];
            NSMutableArray *colorCapitalArray = [colorDic objectForKey:group];
            if (colorCapitalArray == nil) {
                colorCapitalArray = [NSMutableArray arrayWithObject:color];
                [colorDic setObject:colorCapitalArray forKey:group];
            } else {
                [colorCapitalArray addObject:color];
            }
        }
        NSLog(@"%@", colorDic);
    }
#endif
    
    NSArray *arr = @[@"123", @"234", @"345", @"456", @"567"];
    NSMutableArray *mutArr = [NSMutableArray array];
    for (NSInteger i = arr.count - 1; i < arr.count; i--) {
        [mutArr addObject:arr[i]];
       
    }
    NSLog(@"%@", mutArr);
    
    NSDictionary *person = @{@"name" : @"张王", @"sex" : @"男", @"age" : @18};
    NSArray *arrPerson = [person allKeys];
    for (NSInteger i = 0; i < arrPerson.count; i++) {
        NSString *key = arrPerson[i];
        id value = [person objectForKey:key];
        NSLog(@"key = %@ value = %@", key, value);
        
    }
    
    
    
    
    
    
    return 0;
}
